package net.opentsdb.core;

import com.stumbleupon.async.Callback;
import com.stumbleupon.async.Deferred;
import net.opentsdb.query.filter.TagVFilter;
import net.opentsdb.tsd.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author hujf
 * @Date 2022/3/1 10:14
 * @Description 查询数据
 */
public class TsdbQuery implements Query {
    private TSDB tsdb;
    private boolean enabke_fuzzy_filter;
    private boolean use_multi_gets;

    private byte[] metric;

    private List<TagVFilter> filters;

    private static final int UNSET = -1;
    private long start_time = UNSET;
    private long end_time = UNSET;

    public TsdbQuery(TSDB tsdb) {
        this.tsdb = tsdb;
        this.enabke_fuzzy_filter = tsdb.getConfig().getBoolean("tsd.query.enabke_fuzzy_filter");
        this.use_multi_gets = tsdb.getConfig().getBoolean("tsd.query.use_multi_get.enable");
    }


    @Override
    public Deferred<Object> configureFromQuery(TSQuery query, int index) {
        TSSubQuery subQuery = query.getQueries().get(index);
        start_time = query.getStartTime();
        end_time = query.getEndTime();


        class MetricCB implements Callback<Deferred<Object>, byte[]> {

            @Override
            public Deferred<Object> call(byte[] uid) throws Exception {
                metric = uid;
                if (filters != null) {
                  //  return Deferred.group(resolveTagFilters()).addCallback(new FilterCB());
                }
                return null;
            }


            private List<Deferred<byte[]>> resolveTagFilters() {
                final List<Deferred<byte[]>> deferreds = new ArrayList<>(filters.size());
                for(TagVFilter filter:filters){

                }
             //   deferreds.add(filters.resolveTa)
                return deferreds;
            }
        }


        class FilterCB implements Callback<Object, ArrayList<byte[]>> {

            @Override
            public Object call(ArrayList<byte[]> arg) throws Exception {
                findGroupBys();
                return null;
            }


        }


        return tsdb.metrics.getIdAsync(subQuery.getMetric()).addCallbackDeferring(new MetricCB());
    }

    private void findGroupBys() {
    }
}
